package com.gexin.common.job;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.BigDecimalUtils;
import com.gexin.common.utils.BigDecimalUtilsIfNull;
import com.gexin.model.activity.MemberInfo;
import com.gexin.model.activity.MemberLevel;
import com.gexin.model.activity.MemberOrder;
import com.gexin.model.activity.MemberOrderIntegral;
import com.gexin.model.enums.MemberInfoEnum;
import com.gexin.model.order.Order;
import com.gexin.model.order.OrderItem;
import com.gexin.model.order.OrderMaster;
import com.gexin.model.user.UserCoupon;
import com.gexin.model.weixin.UserAddress;
import com.gexin.service.agent.AgentService;
import com.gexin.service.memberAcitivty.IntegralService;
import com.jfinal.log.Log;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 订单7天自动收货定时器
 * 黎峻
 */
public class OrderJob implements Runnable {
    static Log log = Log.getLog(OrderJob.class);

    public void run() {
        //log.info("执行自动收货代码");
        automaticReceipt();
        shutDownOrder();
    }




//    public void teste() {
//        List<MemberOrder> memberOrderList = MemberOrder.dao.findAll();
//        Map<String, List<MemberOrder>> groupBy = memberOrderList.stream().filter(memberOrder ->
//                memberOrder.getOrderId() != null).collect(Collectors.groupingBy(MemberOrder::getOrderId));
//        memberExchage(null, groupBy);
//
//    }


    //自动收货
    public static void automaticReceipt() {
        List<Order> list1 = Order.dao.find("select * from t_order where state=2 and  TO_DAYS(NOW()) - TO_DAYS(express_time) > 7");
        List<MemberOrder> memberOrderList = MemberOrder.dao.findAll();


        for (Order order : list1) {
            log.info("��������������" + order.getId());
            order.setState(Integer.valueOf(3));
            List<OrderItem> orderItemByOrderList = OrderItem.dao.findOrderItemByOrderId(order.getId());
//            integralService.buyGeneralIntegral(order.getUserId(),);
//            integralService.memberExchage(order,groupBy);
            if (!StringUtils.isEmpty(order.getCouponId())) {
                UserCoupon userCoupon = UserCoupon.dao.findByUserIdAndCoupon(order.getUserId(), order.getCouponId());
                if ((userCoupon != null) &&
                        (userCoupon.getUseState().intValue() == 99)) {
                    userCoupon.setUseState(Integer.valueOf(1));
                    userCoupon.update();
                }
            }
            order.setReceiptTime(new Date());
            boolean update = order.update();
            if ((update) &&
                    (order.getAgentId() != null)) {

//              增加积分
                IntegralService integralService = new IntegralService();
                integralService.buyGeneralIntegral(order);

                UserAddress address = (UserAddress) UserAddress.dao.findById(order.getAddressId());

                ApiResponse response = AgentService.dao.fashionable(order.getAgentId(), order.getAgentCityId(), order);
                if (!response.isState()) {
                    log.info(" ���������� �������������������������� ��������" + response.getMsg());
                }
            }
        }
    }


    //待支付订单 超时支付 修改状态
    public static void shutDownOrder() {
        List<Order> order_list = Order.dao.find("select * from t_order where  state =0  and DATE_FORMAT(createtime,'%Y-%m-%d %T') <= SUBDATE(NOW(),interval 20 MINUTE) ");
        for (Order order : order_list) {
            log.info("执行订单待支付超时关闭此订单，订单号：" + order.getId());
            if (order.getState().intValue() == Order.State_Wait_Pay) {
                order.setState(Order.State_Close);
                order.update();
            }
        }
    }
}